package com.IntelligentCommunity.es.util;

import com.IntelligentCommunity.common.core.utils.StringUtils;
import com.IntelligentCommunity.es.domain.EsIndexInfo;
import com.IntelligentCommunity.es.domain.constants.EsConstants;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author 美国五星上将麦克阿瑟
 */
@Component
@Log4j2
public class ESIndexUtil {


    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public boolean indexExist(String indexName) {
        if (StringUtils.isEmpty(indexName)) {
            throw new RuntimeException("indexName不能为空");
        }
        GetIndexRequest indexRequest = new GetIndexRequest(indexName);
        try {
            return restHighLevelClient.indices().exists(indexRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public void createIndex(String indexName, Integer flag) {
        try {
            if (!indexExist(indexName)) {  // 索引不存在
                EsIndexInfo esIndexInfo = parseJsonFile(flag);  // 解析json文件
                esIndexInfo.setIndexName(indexName);  // 设置索引名称
                createWithMappingsAndSettings(esIndexInfo);  // 创建索引
            }
        }catch (Exception e){
            throw new RuntimeException("创建索引错误");
        }
    }




    private EsIndexInfo parseJsonFile(Integer flag) {

        try {

            ClassPathResource classPathResource = null;

            if (flag == 1) {
                classPathResource = new ClassPathResource(EsConstants.FACE_MAPPINGS_FILE);
            }
            if (flag == 2){
                classPathResource = new ClassPathResource(EsConstants.CAR_MAPPINGS_FILE);
            }

            File file = classPathResource.getFile();

            ObjectMapper objectMapper = new ObjectMapper();

            return objectMapper.readValue(file, EsIndexInfo.class);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    private void createWithMappingsAndSettings(EsIndexInfo esIndexInfo) {

        CreateIndexRequest request = new CreateIndexRequest(esIndexInfo.getIndexName());

        // 设置索引映射
        if (ObjectUtils.isNotEmpty(esIndexInfo.getMappings())) {
            request.mapping(esIndexInfo.getMappings());
        }

        try {
            restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("调用es创建索引异常：{}", e);
            throw new RuntimeException("创建索引失败");
        }
    }


}
